package com.aierkeji.call.b2c.entity;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.springframework.util.CollectionUtils;

import com.aierkeji.call.b2c.entity.contants.Gender;
import com.aierkeji.call.b2c.entity.snap.AkjCallUserSnap;
import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * 人员表
 * 
 * @author 陈宇强
 *
 */
@Entity
@Table(name = "akj_call_user")
public class AkjCallUser {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "id")
	private Long id;

	@Column(length = 27)
	private String name;

	@Column(length = 30,unique=true)
	private String account;

	@Column(length = 128)
	private String pwd;

	@Column(length = 128)
	private String userNo;

	@Column(length = 1)
	@Enumerated(EnumType.STRING)
	private Gender gender;

	@Column(columnDefinition = "datetime")
	private Date birth;

	@ManyToOne(targetEntity = AkjCallPosition.class, fetch = FetchType.EAGER)
	@JoinColumn(name = "position_id", nullable = true)
	@NotFound(action = NotFoundAction.IGNORE)
	private AkjCallPosition position;// 职位

	@ManyToOne(targetEntity = AkjCallEnterprise.class, fetch = FetchType.EAGER)
	@JoinColumn(name = "enterprise_id", nullable = true)
	@NotFound(action = NotFoundAction.IGNORE)
	private AkjCallEnterprise enterprise;// 企业

	@Column(name = "is_delete", nullable = false)
	private boolean isDelete = false;

	@Column(columnDefinition = "datetime")
	@JsonIgnore
	private Date updatedAt;

	@Column(columnDefinition = "datetime")
	private Date createdAt;

	@ManyToOne(targetEntity = AkjCallUserSnap.class, fetch = FetchType.EAGER)
	@JoinColumn(name = "updated_by", nullable = true)
	@NotFound(action = NotFoundAction.IGNORE)
	@JsonIgnore
	private AkjCallUserSnap updatedBy;

	@Column(name = "leader_id", length = 32)
	private Long leaderId;// 审批人

	@Column(columnDefinition = "datetime")
	private Date approvedAt;// 审批时间

	@ManyToMany(fetch = FetchType.EAGER)
	@JoinTable(name = "akj_user_role_ref", joinColumns = {
			@JoinColumn(name = "user_id",unique=false) }, inverseJoinColumns = { @JoinColumn(name = "role_id",unique=false) })
	private List<AkjCallRole> roles = new ArrayList<>();
	
	@Transient
	private Set<String> rolesCode = new HashSet<>();
	
	@Column(name = "show_log", nullable = false)
	private boolean show_log = false;
	
	@Column(length = 128)
	private String version;
	
	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public boolean isShow_log() {
		return show_log;
	}

	public void setShow_log(boolean show_log) {
		this.show_log = show_log;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public String getUserNo() {
		return userNo;
	}

	public void setUserNo(String userNo) {
		this.userNo = userNo;
	}

	public Gender getGender() {
		return gender;
	}

	public void setGender(Gender gender) {
		this.gender = gender;
	}

	public Date getBirth() {
		return birth;
	}

	public void setBirth(Date birth) {
		this.birth = birth;
	}

	public AkjCallPosition getPosition() {
		return position;
	}

	public void setPosition(AkjCallPosition position) {
		this.position = position;
	}

	public boolean isDelete() {
		return isDelete;
	}

	public void setDelete(boolean isDelete) {
		this.isDelete = isDelete;
	}

	public AkjCallEnterprise getEnterprise() {
		return enterprise;
	}

	public void setEnterprise(AkjCallEnterprise enterprise) {
		this.enterprise = enterprise;
	}

	public Date getUpdatedAt() {
		return updatedAt;
	}

	public void setUpdatedAt(Date updatedAt) {
		this.updatedAt = updatedAt;
	}

	public Date getCreatedAt() {
		return createdAt;
	}

	public void setCreatedAt(Date createdAt) {
		this.createdAt = createdAt;
	}


	public AkjCallUserSnap getUpdatedBy() {
		return updatedBy;
	}

	public void setUpdatedBy(AkjCallUserSnap updatedBy) {
		this.updatedBy = updatedBy;
	}

	public Long getLeaderId() {
		return leaderId;
	}

	public void setLeaderId(Long leaderId) {
		this.leaderId = leaderId;
	}

	public Date getApprovedAt() {
		return approvedAt;
	}

	public void setApprovedAt(Date approvedAt) {
		this.approvedAt = approvedAt;
	}
	
	
	
	public List<AkjCallRole> getRoles() {
		return roles;
	}

	public void setRoles(List<AkjCallRole> roles) {
		this.roles = roles;
	}

	public Set<String> getRolesCode() {
		return rolesCode;
	}

	public void setRolesCode(Set<String> rolesCode) {
		this.rolesCode = rolesCode;
	}

	public boolean checkRole(String roleCode) {
		if(!CollectionUtils.isEmpty(rolesCode)) {
			return rolesCode.contains(roleCode);
		}
		if(!CollectionUtils.isEmpty(roles)) {
			Optional<AkjCallRole> options = roles.stream().filter(k->k.getCode().equals(roleCode)).findFirst();
			return options.isPresent();
		}
		return false;
	}


}
